<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-07-24 14:13
 *
 * Version: 2.0
 */

namespace App\Modules\Message\Repositories;


use App\Exceptions\NotFoundException;
use App\Models\MessageLog;
use App\Modules\Message\Criterias\GetWxMessageListCriteria;
use App\Modules\Message\Validators\MessageLogValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;
use Illuminate\Support\Facades\DB;

class MessageLogRepository extends BaseRepository
{
	public function model()
	{
		return MessageLog::class;
	}

	public function validator()
	{
		return MessageLogValidator::class;
	}

	/**
	 * 验证是否存在
	 * @param $messageId
	 * @return mixed
	 * @throws NotFoundException
	 */
	public function isExisted($messageId){
		$messageInfo = $this->find($messageId);
		if(empty($messageInfo)){
			throw new NotFoundException(['msg'=>'信息没找到']);
		}
		return $messageInfo;
	}
	/**
	 * 更新已读状态
	 * @param $messageId
	 * @return mixed
	 */
	public function save($messageId)
	{
		return $this->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s',time())],$messageId);
	}

	/**
	 * 获取消息列表
	 * @param $recipient
	 * @param $scene
	 * @param $type
	 * @param $pageSize
	 * @param bool $isApp
	 * @param null $storeId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function getMessageList($recipient, $scene, $type, $pageSize,$isApp=false,$storeId=null)
{
	return $this->pushCriteria(new GetWxMessageListCriteria($recipient, $scene, $type,false,$isApp,$storeId))->paginate($pageSize, ['*'], 'paginate');
}

	/**
	 * 统计个数
	 * @param $recipient
	 * @param $scene
	 * @return array
	 */
	public function countMessageCounts($recipient, $scene)
	{
		return DB::select('SELECT m.type, sum( CASE WHEN l.is_read = 1 THEN 1 ELSE 0 END ) AS read_count, sum( CASE WHEN l.is_read = 0 THEN 1 ELSE 0 END ) AS unread_count  FROM hx_message AS m LEFT JOIN hx_message_log AS l ON m.id = l.message_id  AND l.scene = "'.$scene.'"  AND l.recipient ="'.$recipient.'" GROUP BY m.type;');

	}


	/**
	 * 查询未读消息总数
	 * @param $recipient
	 * @param $scene
	 * @param $type
	 * @param bool $isApp
	 * @param null $storeId
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
	public function countMessageList($recipient, $scene, $type,$isApp=false,$storeId=null)
	{
		return $this->pushCriteria(new GetWxMessageListCriteria($recipient, $scene, $type,true,$isApp,$storeId))->get();
	}


	/**
	 * 读取所有消息
	 * @param $recipient
	 * @param $scene
	 * @return mixed
	 */
	public function readAll($recipient,$scene){
		 return  $this->model->where(['is_read'=>0,'scene'=>$scene,'recipient'=>$recipient])->update(['is_read'=>1,'read_time'=>date('Y-m-d H:i:s',time())]);
	}

	/**
	 * 求未读消息总数
	 * @param $recipient
	 * @param $scene
	 * @return mixed
	 */
	public function getUnreadNum($recipient,$scene){
		return $this->model->where(['is_read'=>0,'scene'=>$scene,'recipient'=>$recipient])->count();
	}
}